package LuoGu.排序.生日;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        String [] name = new String [n];
        int [][] nnn = new int [n][4];
        for(int i=0;i<n;i++){
            name[i] = s.next();
            nnn[i][0] = i;
            nnn[i][1] = s.nextInt();
            nnn[i][2] = s.nextInt();
            nnn[i][3] = s.nextInt();
        }
        Arrays.sort(nnn, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if(o1[1]==o2[1]&&o1[2]!=o2[2]) return o1[2]-o2[2];
                else if(o1[1]==o2[1]&&o1[2]==o2[2]&&o1[3]!=o2[3]) return o1[3]-o2[3];
                else if(o1[1]==o2[1]&&o1[2]==o2[2]&&o1[3]==o2[3]) return o2[0]-o1[0];
                else return o1[1]-o2[1];
            }
        });
        for(int i=0;i<n;i++){
            System.out.println(name[nnn[i][0]]);
        }
    }
}